/**
 * Pool monitor
 */

/**
 * read monitor
 */
var monitor = {
	// 三个实时指标计时器
	timer_read: null, timer_write: null, timer_iops: null,
	
	/**
	 * 
	 */
	clearTimer : function() {
		if (monitor.timer_read) clearInterval(monitor.timer_read);
		if (monitor.timer_write) clearInterval(monitor.timer_write);
		if (monitor.timer_iops) clearInterval(monitor.timer_iops);
	},
	/**
	 * read monitor
	 */
	read : function() {
		monitor.clearTimer();
		/*var param = {}
		var isReal = $("input[name='isReal'][value='1']").is("input:checked");
		isReal = isReal == undefined ? true : isReal;
		var spanTime = $("#spanTime").val();
		if (spanTime) {
			param.spanTime = spanTime;
		}*/			
		$.ajax({
			type : "get",
			url : baseURL + "/monitor/pool/chart_read",
//			data : param,
			dataType : "json",
			success :function(json) {
				
			    $('#read').highcharts({
			    	chart: {
			    		type: 'spline',
			            animation: Highcharts.svg, // don't animate in old IE
			            marginRight: 10,
			            events: {
			                load: function () {
			                    // set up the updating of the chart each 5 seconds
//			                	if (!isReal) return;
			                    var seriesArr = this.series;
			                    monitor.timer_read = setInterval(function () {
			                    	$.ajax({
			                    		type : "get",
			                    		url : baseURL + "/monitor/pool/updated_read",
			                    		dataType : "json",
			                    		beforeSend : function() {},
			                    		success :function(json) {
			                    			if (!json || json.dataset.length == 0) return;
			                    			var updateData = json.dataset;
			                    			var xNames = json.categories;
					                    	for (var i = 0; i < seriesArr.length; i++) {
					    	                    var series = seriesArr[i];
						                        var x = xNames[i];
						                        var yDatas = updateData[i].data;
					    	                    for (var j = 0; j < yDatas.length; j++) {
							                        var y = yDatas[j];
							                        series.addPoint([x,y],true,true);
					    	                    }
					                    	}
			                    		}
			                    	});
			                    }, 10000);
			                }
			            }
			    	},
			        title: {
			            text: localeMsg.pool.read,
			            x: -20 //center
			        },
			        xAxis: {//横坐标
			            categories: json.categories,
			            tickInterval: 60
			        },
			        yAxis: {//纵坐标
			            title: {
			                text: 'Bytes Per Second'
			            },
			            plotLines: [{
			                value: 0,
			                width: 1,
			                color: '#808080'
			            }]
			        },
			        series: json.dataset
			    });
			}
		});
	},
	/**
	 * write monitor
	 */
	write : function(){
		monitor.clearTimer();
		/*var param = {}
		var isReal = $("input[name='isReal'][value='1']").is("input:checked");
		var spanTime = $("#spanTime").val();
		if (spanTime) {
			param.spanTime = spanTime;
		}*/
		$.ajax({
			type : "get",
			url : baseURL + "/monitor/pool/chart_write",
//			data : param,
			dataType : "json",
			success :function(json) {
			    $('#write').highcharts({
			    	chart: {
			    		type: 'spline',
			            animation: Highcharts.svg, // don't animate in old IE
			            marginRight: 10,
			            events: {
			                load: function () {
			                    // set up the updating of the chart each 5 seconds
//			                	if (!isReal) return;
			                    var seriesArr = this.series;
			                    monitor.timer_write = setInterval(function () {
			                    	$.ajax({
			                    		type : "get",
			                    		url : baseURL + "/monitor/pool/updated_write",
			                    		dataType : "json",
			                    		beforeSend : function() {},
			                    		success :function(json) {
			                    			if (!json || json.dataset.length == 0) return;
			                    			var updateData = json.dataset;
			                    			var xNames = json.categories;
					                    	for (var i = 0; i < seriesArr.length; i++) {
					    	                    var series = seriesArr[i];
						                        var x = xNames[i];
						                        var yDatas = updateData[i].data;
					    	                    for (var j = 0; j < yDatas.length; j++) {
							                        var y = yDatas[j];
							                        series.addPoint([x, y], true, true);
					    	                    }
					                    	}
			                    		}
			                    	});
			                    }, 10000);
			                }
			            }
			    	},
			        title: {
			            text: localeMsg.pool.write,
			            x: -20 //center
			        },
			        xAxis: {//横坐标
			            categories: json.categories,
			            tickInterval: 60
			        },
			        yAxis: {//纵坐标
			            title: {
			                text: 'Bytes Per Second'
			            },
			            plotLines: [{
			                value: 0,
			                width: 1,
			                color: '#808080'
			            }]
			        },
			        series: json.dataset
			    });
			}
		});
	},
	/**
	 * operate monitor
	 */
	operate : function(){
		monitor.clearTimer();
		/*var param = {}
		var isReal = $("input[name='isReal'][value='1']").is("input:checked");
		var spanTime = $("#spanTime").val();
		if (spanTime) {
			param.spanTime = spanTime;
		}*/
		$.ajax({
			type : "get",
			url : baseURL + "/monitor/pool/chart_operations",
//			data : param,
			dataType : "json",
			success :function(json) {
			    $('#operate').highcharts({
			    	chart: {
			    		type: 'spline',
			            animation: Highcharts.svg, // don't animate in old IE
			            marginRight: 10,
			            events: {
			                load: function () {
			                    // set up the updating of the chart each 5 seconds
//			                	if (!isReal) return;
			                    var seriesArr = this.series;
			                    monitor.timer_iops = setInterval(function () {
			                    	$.ajax({
			                    		type : "get",
			                    		url : baseURL + "/monitor/pool/updated_operations",
			                    		dataType : "json",
			                    		beforeSend : function() {},
			                    		success :function(json) {
			                    			if (!json || json.dataset.length == 0) return;
			                    			var updateData = json.dataset;
			                    			var xNames = json.categories;
					                    	for (var i = 0; i < seriesArr.length; i++) {
					    	                    var series = seriesArr[i];
						                        var x = xNames[i];
						                        var yDatas = updateData[i].data;
					    	                    for (var j = 0; j < yDatas.length; j++) {
					    	                    	 var y = yDatas[j];
								                        series.addPoint([x, y], true, true);
					    	                    }
					                    	}
			                    		}
			                    	});
			                    }, 10000);
			                }
			            }
			    	},
			        title: {
			            text: localeMsg.pool.operate,
			            x: -20 //center
			        },
			        xAxis: {//横坐标
			            categories: json.categories,
			            tickInterval: 60
			        },
			        yAxis: {//纵坐标
			            title: {
			                text: 'IOPS'
			            },
			            plotLines: [{
			                value: 0,
			                width: 1,
			                color: '#808080'
			            }]
			        },
			        series: json.dataset
			    });
			}
		});
	},
};
